package com.atfusu.controller;

import com.atfusu.bean.entity.Admin;
import com.atfusu.bean.entity.HouseBroker;
import com.atfusu.service.AdminService;
import com.atfusu.service.HouseBrokerService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

/**
 * Title:
 * Author: fusu
 * DateTime:  2022/11/29
 * Description:
 **/
@Controller
@RequestMapping("/houseBroker")
public class HouseBrokerController {

    private static final String PAGE_CREATE = "houseBroker/create";
    private static final String PAGE_SUCCESS = "common/success";

    @DubboReference
    private HouseBrokerService houseBrokerService;

    @DubboReference
    private AdminService adminService;

    @RequestMapping("/create/{houseId}")
    public String create(@PathVariable Long houseId, Map map) {
        //获取除已有经纪人外的所有管理员
        List<Admin> adminList = adminService.findOtherAdminsByHouseId(houseId);

        map.put("houseId", houseId);
        map.put("adminList", adminList);
        return PAGE_CREATE;
    }

    @RequestMapping("/save")
    public String save(HouseBroker houseBroker) {

        houseBrokerService.insert(houseBroker);

        return PAGE_SUCCESS;
    }

    @RequestMapping("/delete/{houseId}/{id}")
    public String delete(@PathVariable Long houseId, @PathVariable Long id) {
        //id为hse_house_broker表的id，用于删除数据
        //houseId为当前房源的id，用于跳转到当前房源详情信息的页面
        houseBrokerService.delete(id);

        return "redirect:/house/show/" + houseId;
    }
}
